package com.sgb.kjwl.view.ui.adapter.self.carMasters;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.self.CarriageSelectEntity;
import com.sgb.kjwl.model.entity.self.SubSetListEntity;
import com.sgb.kjwl.model.entity.transport.SingleItemConstant;
import com.sgb.kjwl.utils.DefaultUtil;
import com.sgb.kjwl.view.widget.MyItemDecoration;
import com.swgk.core.recyclerview.FlowLayoutManager;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 杨畔
 * @date: 4/8/21 3:34 PM
 * @Description: 订阅设置适配器
 */
public class SubscriptionSetAdapter extends BaseQuickAdapter<SubSetListEntity, BaseViewHolder> {
    //标签点击回调
    public CallBack mCallBack;
    private int mMaxNumber = 3;
    private List<SubSetListEntity> data;

    public void addCallBack(CallBack callBack) {
        mCallBack = callBack;
    }

    public SubscriptionSetAdapter(@Nullable List<SubSetListEntity> data) {
        super(R.layout.item_sub_set, data);
        this.data = data;
    }

    @Override
    protected void convert(BaseViewHolder helper, SubSetListEntity item) {
        helper.setText(R.id.tv_item_sub_set_title, DefaultUtil.getString(item.getTitle()));
        if (helper.getLayoutPosition() == data.size() - 1)
            helper.setVisible(R.id.bottom_line, false);
        //设置子列表
        initSubList(helper, item);
    }

    /**
     * 订阅列表
     */
    private void initSubList(BaseViewHolder helper, SubSetListEntity item) {
        RecyclerView recyclerView = helper.getView(R.id.rv_item_sub_set);
        List<CarriageSelectEntity> list = item.getList();
        //判断是否添加选择按钮
        if (list == null) {
            list = new ArrayList<>();
        }
        adjustTag(list);
        //标签列表
        SubTagAdapter adapter = new SubTagAdapter(list);
        recyclerView.setLayoutManager(new FlowLayoutManager());
        if (recyclerView.getItemDecorationCount() == 0) {
            recyclerView.addItemDecoration(new MyItemDecoration());
        }
        adapter.bindToRecyclerView(recyclerView);
        //按钮监听
        List<CarriageSelectEntity> finalList = list;
        adapter.setOnItemChildClickListener((adapter1, view, position) -> {
            if (view.getId() == R.id.fl_item_sub_tag_choose) {
                if (mCallBack != null) mCallBack.addTag(helper.getLayoutPosition());
            } else if (view.getId() == R.id.fl_item_sub_tag_del) {
                finalList.remove(position);
                adjustTag(finalList);
                adapter1.notifyDataSetChanged();
            }
        });
    }

    /**
     * 调整标签（显示/隐藏选择按钮）
     */
    private void adjustTag(List<CarriageSelectEntity> list) {
        if (list.size() == 0) {
            //数量为0时直接添加选择按钮
            list.add(new CarriageSelectEntity(SingleItemConstant.TYPE_TWO));
        } else if (list.size() > mMaxNumber && list.get(list.size() - 1).getItemType() == SingleItemConstant.TYPE_TWO) {
            //大于最大tag数量时移除选择按钮
            list.remove(list.size() - 1);
        } else if (list.size() < mMaxNumber && list.get(list.size() - 1).getItemType() != SingleItemConstant.TYPE_TWO) {
            //小于最大tag数量时添加选择按钮
            list.add(new CarriageSelectEntity(SingleItemConstant.TYPE_TWO));
        }
    }

    /**
     * 订阅标签
     */
    public static class SubTagAdapter extends BaseQuickAdapter<CarriageSelectEntity, BaseViewHolder> {

        public SubTagAdapter(@Nullable List<CarriageSelectEntity> data) {
            super(R.layout.item_sub_tag, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, CarriageSelectEntity item) {
            if (item.getItemType() == SingleItemConstant.TYPE_ONE) {
                helper.setText(R.id.tv_item_sub_tag_name, DefaultUtil.getString(item.getName()))
                        .setVisible(R.id.ll_item_sub_tag, true)
                        .setVisible(R.id.fl_item_sub_tag_choose, false)
                        .addOnClickListener(R.id.fl_item_sub_tag_del);
            } else if (item.getItemType() == SingleItemConstant.TYPE_TWO) {
                helper.setVisible(R.id.ll_item_sub_tag, false)
                        .setVisible(R.id.fl_item_sub_tag_choose, true)
                        .addOnClickListener(R.id.fl_item_sub_tag_choose);
            }
        }
    }

    /**
     * 回调接口
     */
    public interface CallBack {
        void addTag(int parentIndex);
    }
}
